/**
 * Created by meiguizhinian on 2018/7/20
 * Copyright © 2018 lixiaolong. All rights reserved.
 */

//TODO：等待完成
//cocos2dx class
ivy.UIRichTextSimple = ivy.UILabelBase.extend({
	/**
	 * UIRichTextSimple(const std::string& formName, const std::string& blockName, const cc::UISourceControlData& sourceControlData);
 	 * UIRichTextSimple(const std::string& text, const cocos2d::Rect& showRect, bool ignoreContentSize, float verticalSpace);
	 */
	ctor: function () {
		this._super();
		if (arguments.length>0) {
			this.initWith(...arguments);
		}
	},

	/**
	 * void initWith(const std::string& formName, const std::string& blockName, const cc::UISourceControlData& sourceControlData);
	 * void initWith(const std::string& text, const cocos2d::Rect& showRect, bool ignoreContentSize, float verticalSpace);
	 */
	initWith : function() {
		this.init();
		if (cc.isString(arguments[1])) {
			let formName = arguments[0];
			let blockName = arguments[1];
			let sourceControlData = arguments[2];

			let uim = ivy.UIManager.getInstance();
			let x = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.ePosX);
			let y = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.ePosY);
			let w = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eWidth);
			let h = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eHeight);
			let layerID = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eLayerID);
			this._layerIDInEditor = layerID;
			this.width_ = w;
			this.height_ = h;
			this.setSrcRect(cc.rect(x, y, w, h));
			this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(x + w / 2.0, y + h / 2.0)));
			this.setSrcPosition(this.getPosition());
			this.setContentSize(cc.size(w, h));
			let anchorType = uim.getControlIntDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_RichTextSimple_Anchor_list]);
			this._enterActionID.setValue(uim.getControlActionDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_RichTextSimple_EnterAction_action]));
			this._outActionID.setValue(uim.getControlActionDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_RichTextSimple_OutAction_action]));
			this.setUIElementName(formName, blockName);

			let showText = uim.getControlStringDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_RichTextSimple_ShowText_string]);
			this.ignoreContentSize_ = uim.getControlListDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_RichTextSimple_IgnoreContentSize_list])===1;
			let defaultFontFace= uim.getControlStringDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_RichTextSimple_DefaultFontFace_string]);
			let defaultFontColorInt= uim.getControlColorDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_RichTextSimple_DefaultFontColor_color]);
			let defaultFontSize = uim.getControlFloatDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_RichTextSimple_DefaultFontSize_float]);
			let verticalSpace = uim.getControlFloatDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_RichTextSimple_VerticalSpace_float]);
			this.autoScroll_ = uim.getControlListDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_RichTextSimple_AutoScroll_list]) === 1;
			showText = ivy.MultiLanguageManager.getInstance().getString(showText, ivy.MultiLanguageManager.InterpretDataType.IDT_UIData, true);
			this.verticalSpace_ = verticalSpace;
			this.richTextBox_ = new ccui.RichText();
			this.richTextBox_.setContentSize(this.getContentSize());

			this.richTextBox_.setFontFace(defaultFontFace);
			this.richTextBox_.setFontSize(defaultFontSize);
			this.richTextBox_.setFontColor(this.richTextBox_.stringWithColor3B(ivy.Tools.getColor3BFromInt32(defaultFontColorInt)));
			this.richTextBox_.setVerticalSpace(verticalSpace);
			this.richTextBox_.setCascadeColorEnabled(true);
			this.richTextBox_.setCascadeOpacityEnabled(true);

			this.addAniPlayerTagDescription();
			if (this.autoScroll_) {
				this.listView_ = new ccui.ListView();
				this.listView_.setMagneticAllowedOutOfBoundary(false);
				this.listView_.setCascadeColorEnabled(true);
				this.listView_.setCascadeOpacityEnabled(true);
				this.listView_.setClippingType(ccui.Layout.CLIPPING_SCISSOR);
				if(this.ignoreContentSize_)
					this.listView_.setDirection(ccui.ScrollView.DIR_HORIZONTAL);
				else
					this.listView_.setDirection(ccui.ScrollView.DIR_VERTICAL);
				this.listView_.setBounceEnabled(true);
				this.listView_.setScrollBarEnabled(true);
				this.listView_.setTouchEnabled(true);
				this.listView_.setContentSize(cc.size(w, h));
				//rt->formatText();
				this.listView_.setPosition(cc.p(-(this.width_>>1),-(this.height_>>1)));
				this.layout_ = new ccui.Layout();
				this.layout_.setTouchEnabled(true);
				this.layout_.setCascadeColorEnabled(true);
				this.layout_.setCascadeOpacityEnabled(true);
				this.layout_.addChild(this.richTextBox_);
				this.listView_.pushBackCustomItem(this.layout_);
				this.addChild(this.listView_);
			} else {
				this.addChild(this.richTextBox_);
			}
			this.setString(showText);

			this.setAnchorType(anchorType);

			//////////////////////////////////////////////////////////////////////////
			//设置初始可视性 wsq-2017-3-7
			let isNotVisble = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eNotVisble);
			if (isNotVisble === 1) {
				this.setVisible(false);
			}
		} else {
			let text = arguments[0];
			let showRect = arguments[1];
			let ignoreContentSize = arguments[2];
			let verticalSpace = arguments[3];

			this._layerIDInEditor = 0;
			this.width_ = showRect.size.width;
			this.height_ = showRect.size.height;
			this.setSrcRect(showRect);
			this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(cc.rectGetMidX(showRect), cc.rectGetMidY(showRect))));
			this.setSrcPosition(this.getPosition());
			this.setContentSize(showRect.size);
			this.setUIElementName("", "");
			this.richTextBox_ = new cc.RichText();
			this.richTextBox_.setContentSize(this.getContentSize());
			this.richTextBox_.setVerticalSpace(verticalSpace);
			this.verticalSpace_ = verticalSpace;
			this.richTextBox_.setCascadeColorEnabled(true);
			this.richTextBox_.setCascadeOpacityEnabled(true);
			this.ignoreContentSize_ = ignoreContentSize;
			this.addAniPlayerTagDescription();
			this.addChild(this.richTextBox_);
			this.setString(text);
			this.setAnchorType(ivy.UIBase.UIAnchorType.eAnchorHV);
		}
	},


	setFontColor : function(color) {
		if (this.richTextBox_) {
			//this.richTextBox_.setFontColor(this.richTextBox_.stringWithColor3B(color));
		}
	},
	setAnchorType : function(type) {
		this._super(type);
		let rect;
		let size;
		if (!this.getParent()) {
			rect = cc.rect(this.getSrcRect());
			size = cc.size(rect);
		} else {
			rect = cc.rect(this.getSrcRect());
			size = cc.size(rect);
			let parent = this.getParent();
			if (parent instanceof ivy.UIBase) {
				let rect1= parent.getSrcRect();
				if (rect1.x===0 && rect1.y===0 && rect1.width === 0 && rect1.height === 0) {
				} else {
					let pos22 = cc.p(rect1.x + rect1.width / 2, rect1.y + rect1.height / 2);
					rect.x -= pos22.x;
					rect.y -= pos22.y;
					rect.y = -rect.y;
					let p = ivy.Tools.ConvertPointFromGL(cc.p(rect));
					rect.x = p.x;
					rect.y = p.y;
				}
			}
		}
		this.setContentSize(size);
		if (this.autoScroll_) {
			this.richTextBox_.formatText();
			let rerc = this.richTextBox_.getRenderElementsRowCount();
			let brSpaceV = rerc * this.verticalSpace_;
			this.layout_.setContentSize(this.richTextBox_.getRealSize() + cc.size(0, brSpaceV));
			if (this.ignoreContentSize_) {
				this.richTextBox_.setPosition(cc.p(this.richTextBox_.getRealSize().width / 2, this.richTextBox_.getRealSize().height - (this.height_ >> 1) + brSpaceV));
			} else {
				this.richTextBox_.setPosition(cc.p(width_ >> 1, this.richTextBox_.getRealSize().height - (height_ >> 1) + brSpaceV));
			}
			this.listView_.forceDoLayout();

			this.listView_.setInnerContainerSize(cc.size(this.width_, this.richTextBox_.getRealSize().height));
			this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(rect.x + size.width / 2.0, rect.y + size.height / 2.0)));
		} else {
			switch (type) {
				case ivy.UIBase.eAnchorLT: {
					if (this.richTextBox_) {
						this.richTextBox_.formatText();
						let realsize = this.getRealSize();
						this.richTextBox_.setPosition(cc.p(size.width/2.0, -size.height/2.0));
					}
					this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(rect)));
					break;
				}
				case ivy.UIBase.eAnchorHV://ok
				default: {
					if (this.richTextBox_) {
						this.richTextBox_.formatText();
						let realsize = this.getRealSize();
						this.richTextBox_.setPosition(cc.p(size.width / 2.0 - realsize.width/2.0, -size.height / 2.0+realsize.height/2.0));
					}
					this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(rect.x + size.width / 2.0, rect.y + size.height / 2.0)));
					break;
				}
			}
		}
		this.setSrcPosition(this.getPosition());
	},
	/**
	 * setString(const std::string& content) override;
	 * setString(const std::string& content, bool ignoreAnchor);
	 * setString(int32_t text) override;
	 */
	setString : function(text, ignoreAnchor=null) {
		if (cc.isNumber(text)) {
			text = text.toString();
		}
		if (ignoreAnchor) {
			let mlm = ivy.MultiLanguageManager.getInstance();
			let showText = mlm.getString(content, ivy.MultiLanguageManager.InterpretDataType.IDT_UIData, true);
			this.richTextBox_.removeAllElement();
			this.richTextBox_.initWithXML(showText);
			this.richTextBox_.ignoreContentAdaptWithSize(this.ignoreContentSize_);
			this.richTextBox_.formatText();
		} else {
			if (this.richTextBox_) {
				let mlm = ivy.MultiLanguageManager.getInstance();
				let showText = mlm.getString(content, ivy.MultiLanguageManager.InterpretDataType.IDT_UIData, true);
				this.richTextBox_.removeAllElement();
				this.richTextBox_.initWithXML(showText);
				this.richTextBox_.ignoreContentAdaptWithSize(this.ignoreContentSize_);
				this.richTextBox_.formatText();
				this.setAnchorType(this.getAnchorType());
			}
		}
	},

	getString : function() {
		return this.content_;
	},
	getRenderElementsRowCount : function() {
		if (this.richTextBox_) {
			return this.richTextBox_.getRenderElementsRowCount();
		}
		return 0;
	},
	getRealSize : function() {
		if(this.richTextBox_) {
			let rerc = this.richTextBox_.getRenderElementsRowCount();
			let brSpaceV = rerc*this.verticalSpace_;
			this.realSize_ = cc.size(this.richTextBox_.getRealSize().width, this.richTextBox_.getRealSize().height + brSpaceV);
		} else {
			this.realSize_ = cc.size(0,0);
		}
		return this.realSize_;
	},

	insertElement : function(element, index) {
		if (this.richTextBox_) {
			this.richTextBox_.insertElement(element,index);
		}
	},
	pushBackElement : function(element) {
		if (this.richTextBox_) {
			this.richTextBox_.pushBackElement(element);
		}
	},
	removeElement : function(indexOrElement) {
		this.richTextBox_.removeElement(indexOrElement);
	},
	removeAllElement : function() {
		if (this.richTextBox_) {
			this.richTextBox_.removeAllElement();
		}
	},
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////protected:
	addAniPlayerTagDescription : function() {
		// let rt = this.richTextBox_;
		// cc::RichText::setTagDescription("aniplayer", false, function(tagAttrValueMap) {
		// 	let name = "";
		// 	let actionId = -1;
		// 	let scaleX = 1.0;
		// 	let scaleY = 1.0;
		// 	let color = cc.color.WHITE;
		// 	if (tagAttrValueMap["name"]) {
		// 		name = tagAttrValueMap["name"].asString();
		// 	}
		// 	if (tagAttrValueMap["color"]) {
		// 		color = rt.color3BWithString(tagAttrValueMap["color"].asString());
		// 	}
		// 	if (tagAttrValueMap["action"]) {
		// 		actionId = tagAttrValueMap["action"].asInt();
		// 	}
		// 	if (tagAttrValueMap["scaleX"]) {
		// 		scaleX = tagAttrValueMap["scaleX"].asFloat();
		// 	}
		// 	if (tagAttrValueMap["scaleY"]) {
		// 		scaleY = tagAttrValueMap["scaleY"].asFloat();
		// 	}
		// 	let ele = null;//cc::RichElement*
		// 	if (name.length > 0 && actionId >= 0)
		// 	{
		// 		let player = ivy.AniPlayer.create(name, actionId);
		// 		let colbox = player.getCollisionBox();
		// 		player.setColor(color);
		// 		let width = colbox.size.width*scaleX;
		// 		let height = colbox.size.height*scaleY;
		// 		player.setContentSize(cc.size(width, height));
		// 		player.setScaleX(scaleX);
		// 		player.setScaleY(scaleY);
		// 		player.setPosition(width/2, height/2);
		// 		let node = new cc.Node();
		// 		node.addChild(player);
		// 		node.setContentSize(cc.size(width, height));
		// 		ele = cc::RichElementCustomNode::create(0, cc.color.WHITE, 255, node);
		// 		return std::make_pair(cocos2d::ValueMap(), ele);
		// 	} else {
		// 		ele = cc::RichElementNewLine::create(0, cc.color.WHITE, 255);
		// 		return std::make_pair(cocos2d::ValueMap(), ele);
		// 	}
		// });
	},
	autoScroll_ : false,
	ignoreContentSize_ : false,
	content_ : "",
	richTextBox_: null,//cc::RichText*
	listView_ : null,//cocos2d::ui::ListView*
	layout_ : null,//cocos2d::ui::Layout*
	width_ : 0,
	height_ : 0,
	verticalSpace_ : 0.0,
	realSize_ : null,//cc.size
});

ivy.UIRichTextSimple.create = function () {
	return new ivy.UIRichTextSimple(...arguments);
};
